<template>
  <page-header-wrapper>
    <template v-slot:content>
      <div class="page-header-content">
        <div class="avatar">
          <a-avatar size="large" :src="`${avatarBaseUrl}${userInfo.avatar}`" />
        </div>
        <div class="content">
          <div class="content-title">
            {{ timeFix }}，{{ userInfo.username }}<span class="welcome-text">，{{ welcome }}</span>
          </div>
          <div>{{ userInfo.role | filterRole }} | xxxxxx公司 - Robin进销存平台</div>
        </div>
      </div>
    </template>

    <div>
      <a-row :gutter="24">
        <a-col :xl="16" :lg="24" :md="24" :sm="24" :xs="24">
          <a-card
            class="project-list"
            :loading="loading"
            style="margin-bottom: 24px;"
            :bordered="false"
            title="进行中的订单"
            :body-style="{ padding: 0 }"
          >
            <a slot="extra" @click="skip('/purchase/purchaseOrder')">全部订单</a>
            <div>
              <a-card-grid class="project-card-grid" :key="i" v-for="(item, i) in projects">
                <a-card :bordered="false" :body-style="{ padding: 0 }">
                  <a-card-meta>
                    <div slot="title" class="card-title">
                      <a-avatar size="small" icon="shopping-cart"/>
                      <a>{{ item.purchaser }}</a>
                    </div>
                    <div slot="description" class="card-description">
                      订单编号：{{ item.code }}, <br>
                      该订单创建于{{ item.purchaseTime }}
                    </div>
                  </a-card-meta>
                  <div class="project-item">
                    <a @click="skip('/purchase/purchaseOrder')">审批人员：{{ item.auditor }}</a>
                    <span class="datetime">{{ item.status | filterStatus }}</span>
                  </div>
                </a-card>
              </a-card-grid>
            </div>
          </a-card>

          <a-card :loading="loading" title="动态" :bordered="false">
            <a-list>
              <a-list-item :key="index" v-for="(item, index) in activities">
                <a-list-item-meta>
                  <a-avatar slot="avatar" size="small" :src="item.user.avatar" />
                  <div slot="title">
                    <span>{{ item.user.nickname }}</span
                    >&nbsp; 在&nbsp;<a href="#">{{ item.project.name }}</a
                    >&nbsp; <span>{{ item.project.action }}</span
                    >&nbsp;
                    <a href="#">{{ item.project.event }}</a>
                  </div>
                  <div slot="description">{{ item.time }}</div>
                </a-list-item-meta>
              </a-list-item>
            </a-list>
          </a-card>
        </a-col>
        <a-col
          style="padding: 0 12px"
          :xl="8"
          :lg="24"
          :md="24"
          :sm="24"
          :xs="24">
          <a-card
            title="快速开始 / 便捷导航"
            style="margin-bottom: 24px"
            :bordered="false"
            :body-style="{ padding: 0 }"
          >
            <div class="item-group" v-if="role === 'root'">
              <a @click="skip('/systemManage/userManage')">用户管理</a>
              <a @click="skip('/systemManage/clientManage')">客户管理</a>
              <a @click="skip('/systemManage/roleManage')">角色管理</a>
            </div>
            <div class="item-group" v-if="role === 'auditor'">
              <a @click="skip('/audit/auditPurchase')">采购审批</a>
              <a @click="skip('/audit/auditSale')">销售审批</a>
            </div>
            <div class="item-group" v-if="role === 'purchaser'">
              <a @click="skip('/purchase/purchaseOrder')">采购订单</a>
              <a @click="skip('/purchase/purchaseAdd')">采购</a>
            </div>
            <div class="item-group" v-if="role === 'salesPerson'">
              <a @click="skip('/sale/purchaseOrder')">销售订单</a>
              <a @click="skip('/sale/saleAdd/:sale?')">销售</a>
              <a @click="skip('/sale/saleRejectManage')">退货订单</a>
              <a @click="skip('/sale/saleReject')">退货</a>
            </div>
            <div class="item-group" v-if="role === 'account'">
              <a @click="skip('/purchase/purchaseOrder')">采购审批</a>
              <a @click="skip('/purchase/purchaseOrder')">销售审批</a>
            </div>
            <div class="item-group" v-if="role === 'repoManager'">
              <a @click="skip('/repo/purchaseInRepo')">入库</a>
              <a @click="skip('/repo/purchaseInRepoManage')">入库管理</a>
              <a @click="skip('/repo/purchaseOutRepo')">退货</a>
              <a @click="skip('/repo/purchaseOutRepoManage')">退货管理</a>
              <a @click="skip('/repo/saleInRepo')">退库</a>
              <a @click="skip('/repo/saleInRepoManage')">退库管理</a>
              <a @click="skip('/repo/saleOutRepo')">出库</a>
              <a @click="skip('/repo/saleOutRepoManage')">出库管理</a>
            </div>
          </a-card>
          <a-card :loading="loading" title="团队成员" :bordered="false">
            <div class="members">
              <a-row>
                <a-col :span="12" v-for="(item, index) in userList" :key="index">
                  <a>
                    <a-avatar size="small" :src="`${avatarBaseUrl}${item.avatar}`" />
                    <span class="member">{{ item.username }}</span>
                    <span class="member" style="margin-left: 5px">({{ item.role | filterRole }})</span>
                  </a>
                </a-col>
              </a-row>
            </div>
          </a-card>
        </a-col>
      </a-row>
    </div>
  </page-header-wrapper>
</template>

<script>
import { timeFix } from '@/utils/util'
import { mapState } from 'vuex'
import { PageHeaderWrapper } from '@ant-design-vue/pro-layout'
import { orderList } from '@/api/purchase'
import { getUserList } from '@/api/userManage'

export default {
  name: 'Workplace',
  components: {
    PageHeaderWrapper
  },
  data () {
    return {
      timeFix: timeFix(),
      role: this.$store.getters.userInfo.role,
      avatarBaseUrl: 'http://122.9.107.215:8080/common/download?fileName=',
      projects: [],
      loading: true,
      activities: [],
      userList: []
    }
  },
  filters: {
    filterRole (value) {
      switch (value) {
        case 'root': return '管理员'
        case 'purchaser': return '采购员'
        case 'auditor': return '审核员'
        case 'salesPerson': return '销售员'
        case 'account': return '财务员'
        case 'repoManager': return '仓库管理员'
      }
    },
    filterStatus (value) {
      switch (value) {
        case 0: return '已审批'
        case 1: return '未审批'
        case 2: return '待审批'
      }
    }
  },
  computed: {
    ...mapState({
      welcome: state => state.user.welcome
    }),
    userInfo () {
      return this.$store.getters.userInfo
    }
  },
  created () {
  },
  mounted () {
    this.getProjects()
    this.getActivity()
    this.getTeams()
  },
  methods: {
    getProjects () {
      const _this = this
      orderList({
      }).then(function (res) {
        _this.projects = res.data.list
      }).catch(err => {
        _this.$message.error(err.response.data.msg)
      }).finally(() => {
        _this.loading = false
      })
    },
    getActivity () {
      // this.$http.get('/workplace/activity').then(res => {
      //   this.activities = res.result
      // })
    },
    getTeams () {
      const _this = this
      const tmpVar = _this.role === 'root' ? null : _this.role
      getUserList({
        role: tmpVar
      })
      .then(function (res) {
        _this.userList = res.data.list
      }).catch(function (err) {
        _this.$message.error(err.response.data.msg)
      }).finally(function () {
      })
    },
    skip (path) {
      this.$router.push({ path: path })
    }
  }
}
</script>

<style lang="less" scoped>
@import './css/Workplace.less';

.project-list {
  .card-title {
    font-size: 0;

    a {
      color: rgba(0, 0, 0, 0.85);
      margin-left: 12px;
      line-height: 24px;
      height: 24px;
      display: inline-block;
      vertical-align: top;
      font-size: 14px;

      &:hover {
        color: #1890ff;
      }
    }
  }

  .card-description {
    color: rgba(0, 0, 0, 0.45);
    height: 44px;
    line-height: 22px;
    overflow: hidden;
  }

  .project-item {
    display: flex;
    margin-top: 8px;
    overflow: hidden;
    font-size: 12px;
    height: 20px;
    line-height: 20px;

    a {
      color: rgba(0, 0, 0, 0.45);
      display: inline-block;
      flex: 1 1 0;

      &:hover {
        color: #1890ff;
      }
    }

    .datetime {
      color: rgba(0, 0, 0, 0.25);
      flex: 0 0 auto;
      float: right;
    }
  }

  .ant-card-meta-description {
    color: rgba(0, 0, 0, 0.45);
    height: 44px;
    line-height: 22px;
    overflow: hidden;
  }
}

.item-group {
  padding: 20px 0 8px 24px;
  font-size: 0;

  a {
    color: rgba(0, 0, 0, 0.65);
    display: inline-block;
    font-size: 14px;
    margin-bottom: 13px;
    width: 25%;
  }
}

.members {
  a {
    display: block;
    margin: 12px 0;
    line-height: 24px;
    height: 24px;

    .member {
      font-size: 14px;
      color: rgba(0, 0, 0, 0.65);
      line-height: 24px;
      max-width: 100px;
      vertical-align: top;
      margin-left: 12px;
      transition: all 0.3s;
      display: inline-block;
    }

    &:hover {
      span {
        color: #1890ff;
      }
    }
  }
}

.mobile {
  .project-list {
    .project-card-grid {
      width: 100%;
    }
  }

  .more-info {
    border: 0;
    padding-top: 16px;
    margin: 16px 0 16px;
  }

  .headerContent .title .welcome-text {
    display: none;
  }
}
</style>
